package cn.knet.excel.imp.knetexceltool.service;

import cn.knet.excel.imp.knetexceltool.DTO.KnetBgdataCompanyBase;
import cn.knet.excel.imp.knetexceltool.DTO.KnetBgdataKeyword;
import cn.knet.excel.imp.knetexceltool.comm.KnetBgdataCleanRules;
import cn.knet.excel.imp.knetexceltool.dao.KnetBgdataCompanyBaseDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.LongAdder;

@Service
@Slf4j
public class KnetBgdataCompanyBaseService extends KnetBgdataCleanRules {
    @Resource
    private KnetBgdataCompanyBaseDao knetBgdataCompanyBaseDao;


    @Transactional
    public void insert(KnetBgdataCompanyBase knetBgdataCompanyBase) {
        knetBgdataCompanyBaseDao.insert(knetBgdataCompanyBase);
    }

    @Transactional
    public void saveKeyword(KnetBgdataKeyword knetBgdataKeyword) {
        knetBgdataCompanyBaseDao.saveKeyword(knetBgdataKeyword);
    }

    public void saveKeywordList(LongAdder global_kwOkAdder, LongAdder global_kwErrorAdder, LongAdder single_okAdder, LongAdder single_failAdder, List<Map<String, String>> orgList) {
        for (Map<String, String> map : orgList) {
            String org_name_db = map.get("org_name");
            String orgtype_db = map.get("org_type");
            KnetBgdataKeyword knetBgdataKeyword = null;
            try {
                //进行数据清洗
                knetBgdataKeyword = cleanRules(org_name_db,orgtype_db);
                //入库清洗后的元数据
                knetBgdataCompanyBaseDao.saveKeyword(knetBgdataKeyword);
                global_kwOkAdder.increment();
                single_okAdder.increment();
            } catch (Exception e) {
                global_kwErrorAdder.increment();
                single_failAdder.increment();
                e.printStackTrace();
                log.error("======<保存推荐词时异常>****内容[{}]", knetBgdataKeyword);
            }
        }
    }

    public void saveCompanyList(List<KnetBgdataCompanyBase> list, String fileName, LongAdder global_okAdder, LongAdder global_failAdder, LongAdder single_okAdder, LongAdder singgle_failAdder) {
        for (KnetBgdataCompanyBase data : list) {
            data.setBatch(fileName);
            try {
                insert(data);
                global_okAdder.increment();
                single_okAdder.increment();
            } catch (Exception e) {
                e.printStackTrace();
                log.error("******fail_org[{}#{}]", fileName, data.getOrgName());
                global_failAdder.increment();
                singgle_failAdder.increment();
            }
        }
    }

    public List<Map<String, String>> selectBgdataByEveryPage(int startNum, int pageSize) {
        return knetBgdataCompanyBaseDao.selectBgdataByEveryPage(startNum, pageSize);

    }

    public List<Map<String, String>> selectCitysByType() {
        return knetBgdataCompanyBaseDao.selectCitysByType();
    }

    public int selectBgdataSmtCount() {
        return knetBgdataCompanyBaseDao.selectBgdataSmtCount();
    }

}
